/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:21
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.risk.service.impl;

import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.bp.service.BpDepartService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.enums.RiskLevels;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.mh.service.MonitorPointDataWebSocketService;
import com.joysuch.wwyt.risk.bean.RiskAlarmMessageListBean;
import com.joysuch.wwyt.risk.bean.RiskAlarmMessageListSearchBean;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskEvaluationAlarmMesage;
import com.joysuch.wwyt.risk.entity.dsl.QRiskEvaluationAlarmMesage;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.joysuch.wwyt.risk.repository.RiskEvaluationAlarmMesageDao;
import com.joysuch.wwyt.risk.service.RiskEvaluationAlarmMesageService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class RiskEvaluationAlarmMesageServiceImpl implements RiskEvaluationAlarmMesageService {

    @Autowired
    private RiskEvaluationAlarmMesageDao dao;
    @Autowired
    private BpDepartService departService;
    @Autowired
    private RiskDistrictDao districtDao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private MonitorPointDataWebSocketService monitorPointDataWebSocketService;
    @Autowired
    private BpDepartDao departDao;

    @Override
    public void add(RiskEvaluationAlarmMesage entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(RiskEvaluationAlarmMesage entity) throws Exception {
        // RiskEvaluationAlarmMesage content = dao.findById(entity.getId()).get();
        // if (content != null) {
        // // TODO set nessessary properties
        // dao.save(content);
        // }
    }

    @Override
    public Page<RiskEvaluationAlarmMesage> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskEvaluationAlarmMesage findById(Long id) {
        return dao.findById(id).get();
    }

    //风险分区报警来源两种：区域风险评估、作业风险评估
    @Override
    public void createRiskLevelChangeMessage(Long districtId, Long departId, Integer newRiskLevel,
                                             Integer oldRiskLevel, String riskSource, String riskType) {
        // 生成报警消息
        RiskEvaluationAlarmMesage m = new RiskEvaluationAlarmMesage();
        RiskDistrict district = districtDao.findById(districtId).get();
        m.setCode(bizCodeGeneratorFacade.fengXianFengQuBaoJing(district.getOrgCode().substring(0,7),district.getTenentId()));
        m.setDetail("风险等级(" + riskType + ")从\"" + RiskLevels.findRiskLevelName(oldRiskLevel) + "\"升级为\""
                + RiskLevels.findRiskLevelName(newRiskLevel) + "\",风险提升来自\"" + riskSource + "\"");
        m.setRiskDistrictId(districtId);
        m.setRiskLevel(newRiskLevel);
        m.setResponsibleDepartId(departId);
        m.setState(0);
        m.setOrgCode(district.getOrgCode());
        m.setTenentId(district.getTenentId());
        RiskEvaluationAlarmMesage save = dao.save(m);
        //产生区域报警,立即向WebSocket推送报警信息
        monitorPointDataWebSocketService.getDistrictAlarmMessage(Lists.newArrayList(save), "ADD");
    }

    @Override
    public Page<RiskAlarmMessageListBean> findByPage(Pageable page, RiskAlarmMessageListSearchBean condition) {
        Predicate p = null;
        QRiskEvaluationAlarmMesage q = QRiskEvaluationAlarmMesage.riskEvaluationAlarmMesage;
        if (condition.getNameOrCode() != null) {
            p = ExpressionUtils.or(q.code.like("%" + condition.getNameOrCode() + "%"),
                    q.detail.like("%" + condition.getNameOrCode() + "%"));
        }
        if (condition.getState() != null) {
            BooleanExpression c = q.state.eq(condition.getState());
            p = p == null ? c : ExpressionUtils.and(p, c);
        }

        Page<RiskEvaluationAlarmMesage> pageData = p == null ? dao.findAll(page) : dao.findAll(p, page);

        return PageDataUtils.convertPageData(pageData,
                new PageDataConvert<RiskAlarmMessageListBean, RiskEvaluationAlarmMesage>() {

                    @Override
                    public RiskAlarmMessageListBean convert(RiskEvaluationAlarmMesage f) {
                        RiskAlarmMessageListBean b = new RiskAlarmMessageListBean();
                        b.setId(f.getId());
                        b.setDepartId(f.getResponsibleDepartId());
                        b.setRiskDistrictId(f.getRiskDistrictId());
                        b.setRiskLevel(f.getRiskLevel());
                        b.setRiskLevelName(RiskLevels.findRiskLevelName(f.getRiskLevel()));
                        b.setState(f.getState());
                        b.setCode(f.getCode());
                        b.setRiskDistrictName(findDistrictName(f.getRiskDistrictId()));
                        b.setDepartName(findDepartName(f.getResponsibleDepartId()));
                        b.setContent(f.getDetail());
                        b.setTime(SimpleDateFormatCache.getYmdhms().format(f.getCreateTime()));
                        return b;
                    }
                });
    }

    protected String findDepartName(Long departId) {
        if (departId == null) {
            return null;
        }
        return departService.findById(departId).getName();
    }

    protected String findDistrictName(Long riskDistrictId) {
        if (riskDistrictId == null) {
            return null;
        }
        return districtDao.getOne(riskDistrictId).getName();
    }

}
